<script>
import Vue from 'vue';

function getWatch() {
  const keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  const watch = {};
  keys.forEach(function (k) {
    watch[k] = function (value) {
      this._proxyVm._data[k] = value;
    };
  });
  return watch;
}

export default {
  name: 'ConfigProvider',
  provide() {
    const _self = this;
    this._proxyVm = new Vue({
      data: function data() {
        return Object.assign({}, _self.$props, {
          getPrefixCls: _self.getPrefixCls,
        });
      },
    });
    return {
      configProvider: this._proxyVm._data,
    };
  },
  props: {
    prefixCls: String,
    themes: Object,
    i18nRender: Function,
    setHeader: Function,
  },
  watch: Object.assign({}, getWatch(['prefixCls', 'themes', 'i18nRender', 'setHeader'])),
  methods: {
    getPrefixCls(suffixCls, customizePrefixCls) {
      const _$props$prefixCls = this.prefixCls;
      const prefixCls = _$props$prefixCls === undefined ? 'lj' : _$props$prefixCls;

      if (customizePrefixCls) return customizePrefixCls;
      return suffixCls ? prefixCls + '-' + suffixCls : prefixCls;
    },
  },
  render() {
    return this.$slots.default;
  },
};
</script>
